<?php
class ControllerPaymentEpaylinks extends Controller {
	protected function index() {
		$this->language->load('payment/pp_standard');

		$this->data['text_testmode'] = $this->language->get('text_testmode');		

		$this->data['button_confirm'] = $this->language->get('button_confirm');

		$this->load->model('checkout/order');

		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
		
		$total = $order_info['total'];

		if ($total > 0) {
			$this->data['products'][] = array(
				'name'     => $this->language->get('text_total'),
				'model'    => '',
				'price'    => $total,
				'quantity' => 1,
				'option'   => array(),
				'weight'   => 0
			);	
		} else {
			$this->data['discount_amount_cart'] -= $total;
		}
			
		/*
		 * ePayLinks Integration
		 * 
		 * v1.2 by GeeHim
		 *  
		 */	

		$parameters = array();
		
		$parameters['partner'] = $this->config->get('epaylinks_id');
		$parameters['key'] = $this->config->get('epaylinks_key');
		$parameters['out_trade_no'] = $this->session->data['order_id'];
		$parameters['total_fee'] = round($order_info['total'], 2);
		$parameters['return_url'] = HTTP_SERVER . 'index.php?route=payment/epaylinks/callback';
		$parameters['notify_url'] = HTTP_SERVER . 'index.php?route=payment/epaylinks/callback';
		
		if ($order_info['currency_code'] == "CNY") {
			$parameters['currency_type'] = 'RMB';
		} elseif ($order_info['currency_code'] == "USD" || $order_info['currency_code'] == "HKD") {
			$parameters['currency_type'] = $order_info['currency_code'];
		} else {
			$this->data['error'] = $this->language->get('error_currency');
		}
		
		$parameters['order_create_ip'] = '';
		$parameters['version'] = '3.0';
		$parameters['sign_type'] = 'SHA256';
		$parameters['pay_id'] = '';
		$parameters['base64_memo'] = base64_encode($this->config->get('epaylinks_memo'));

		// Build Sign
		$sign = $this->buildRequestSign($parameters);
		
		$parameters['sign'] = $sign;		
		
		$this->data['request_url'] = $this->getRequestURL($parameters);
		
		$this->data['test'] = "test data";
		 
		 
		$this->data['cancel_return'] = $this->url->link('checkout/checkout', '', 'SSL');
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/epaylinks.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/payment/epaylinks.tpl';
		} else {
			$this->template = 'default/template/payment/epaylinks.tpl';
		}

		$this->render();

	}
						
	function getRequestURL($parameters) {
		
		$req_par = "";
		ksort($parameters);
		foreach($parameters as $k => $v) {
			$req_par .= $k . "=" . urlencode($v) . "&";
		}
		
		$req_par = substr($req_par, 0, strlen($req_par)-1);
		
		$request_url = "http://219.136.207.190:80/paycenter/v2.0/getoi.do" . "?" . $req_par;
		
		return $request_url;
		
	}

	function buildRequestSign($parameters) {
		
		$sign_original_string = "";
		ksort($parameters);
		foreach($parameters as $k => $v) {
			if("" != $v && "sign" != $k) {
				$sign_original_string .= $k . "=" . $v . "&";
			}
		}
		$sign_original_string .= "key=" . $this->config->get('epaylinks_key');
		$sign = strtolower(hash("sha256",$sign_original_string));
		
		return $sign;
	}


	public function callback() {		
		$get_parameters = $this->request->get;
		
		foreach ($get_parameters as $items => $value) {
			$this->data['parameters'][$items] = $value;
		}

		$this->load->model('checkout/order');
		
		$order_status_id = $this->config->get('config_order_status_id');
		
		$order_id = $this->data['parameters']['out_trade_no'];
		
		$order_info = $this->model_checkout_order->getOrder($order_id);
		
		$this->model_checkout_order->confirm($order_id, 5);
		
		$this->model_checkout_order->update($order_id, 5);
		
		// Success page redirect
		$this->response->redirect($this->url->link('checkout/success'));
		
	}
}
?>